﻿<?php

App::uses('PrivadoController', 'Controller');

class UsersController extends PrivadoController
{

    public function changePass()
    {
        $changed = null;
        if ($this->request->is('post'))
        {
            $changed = true;
            $this->loadModel('User');
            $params = $this->request->data['User'];
            $user = array();
            $user = $this->User->getUserByName($params['name']);
            if (count($user) != 0)
            {
                if ($user[0]['User']['type'] == 3)
                {
                    $user[0]['User']['pass'] = $params['pass'];
                    try
                    {
                        $this->User->save($user[0]);
                    }
                    catch (PDOException $e)
                    {
                        $changed = false;
                    }
                }
                else
                {
                    $changed = false;
                }
            }
            else
            {
                $changed = false;
            }
        }
        $this->set('changed', $changed);
        $this->set('title', 'Cambio de contraseña');
        $this->set('description', 'Usuarios');
    }

    public function createAdmin()
    {
        $createUser = null;
        if ($this->request->is('post'))
        {
            $createUser = true;
            $params = $this->request->data['User'];
            $newUser = array();
            $newUser['user'] = $params['name'];
            $newUser['pass'] = $params['pass'];
            $newUser['type'] = 3; #Administrativo
            $newUser['status'] = 1; #activo
            $this->loadModel('User');
            try
            {
                $this->User->save($newUser);
            }
            catch (PDOException $e)
            {
                $createUser = false;
            }
        }

        $this->set('createUser', $createUser);
        $this->set('title', 'Crear usuario administrativo');
        $this->set('description', 'Usuarios');
    }

}

